package com.seari.client;

import com.seari.service.AppCacheService;
import com.seari.service.OperateService;
import com.seari.service.PassengerFlowService;
import com.seari.service.TicketingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @ClassName PronClient
 * @Description 定时获取数据
 * @Author LANDSUS
 * @Date 2021/6/2 17:05
 * @Version 1.0
 */
@Component("PronClient")
public class PronClient extends BaseClient {

    protected static Logger log = LoggerFactory.getLogger(PronClient.class);

    @Autowired
    private OperateService operateService;

    @Autowired
    private TicketingService ticketingService;

    @Autowired
    private PassengerFlowService passengerFlowService;

    @Autowired
    private AppCacheService cacheService;


    @Override
    public void process() throws Exception {
        this.shangHaiWeather();
        this.ticketPercent();
        this.stationFlow();
        this.stationTodayTotal();
        this.totalDate();
        this.totalInfo4DifferentSide();
        this.trulyOnLine4District();
        this.districtFlow();
        this.trulyOnLine4NewDistrict();
        this.newDistrictFlow();
        this.newDistrictDisplayBoard();
        this.aboutJB();
        this.monitor6station();
    }


    // 上海全市天气，时效性要求低
    private void shangHaiWeather(){
        Integer integer = operateService.shangHaiWeather();
        cacheService.refreshCache("shangHaiWeather", integer);
        log.info("shangHaiWeather save cache successfully");
    }

    // 票务百分比，时效性要求较低，数据库为15分钟刷新一次
    private void ticketPercent(){
        Map<String, String> ticketing = ticketingService.getTicketing();
        cacheService.refreshCache("ticketPercent", ticketing);
        log.info("ticketPercent save cache successfully");
    }

    // 车站实时进出站，时效性普通：5分钟
    private void stationFlow(){
        List<Map<String, Object>> stationFlow = passengerFlowService.stationFlow();
        cacheService.refreshCache("stationFlow", stationFlow);
        log.info("stationFlow save cache successfully");
    }

    // 车站今日总进出站，时效性普通
    private void stationTodayTotal(){
        List<Map<String, Object>> in = passengerFlowService.stationFlowTodayTotal("in");
        cacheService.refreshCache("stationTodayTotalIn", in);
        log.info("stationTodayTotalIn save cache successfully");
        List<Map<String, Object>> out = passengerFlowService.stationFlowTodayTotal("out");
        cacheService.refreshCache("stationTodayTotalOut", out);
        log.info("stationTodayTotalOut save cache successfully");
    }

    // 今日统计数据，时效性普通
    private void totalDate(){
        Map totalDate = null;
        try{
            totalDate = passengerFlowService.totalDate();

        }catch(NullPointerException e) {
            log.error(e.getMessage());
        }
        if(totalDate != null && totalDate.size() > 0){
            cacheService.refreshCache("totalDate", totalDate);
            log.info("totalDate save cache successfully");
        }else {
            log.error("totalDate get failed,doesn't save cache");
        }
    }

    // 区、新城、站维度的今日统计数据，时效性普通
    private void totalInfo4DifferentSide(){
        // 区维度
        List<Map<String, Object>> d = passengerFlowService.total4Station("d");
        cacheService.refreshCache("totalInfo4DifferentSideD", d);
        log.info("totalInfo4DifferentSideD save cache successfully");

        // 站维度
        List<Map<String, Object>> s = passengerFlowService.total4Station("s");
        cacheService.refreshCache("totalInfo4DifferentSideS", s);
        log.info("totalInfo4DifferentSideS save cache successfully");

        // 新城维度
        List<Map<String, Object>> n = passengerFlowService.total4Station("n");
        cacheService.refreshCache("totalInfo4DifferentSideN", n);
        log.info("totalInfo4DifferentSideN save cache successfully");
    }

    // 区的实时在网客流，时效性普通
    private void trulyOnLine4District(){
        Map true4District = passengerFlowService.true4District();
        cacheService.refreshCache("trulyOnLine4District", true4District);
        log.info("trulyOnLine4District save cache successfully");
    }

    // 行政区实时进出站客流，时效性普通
    private void districtFlow(){
        List<Map<String, Object>> districtFlow = passengerFlowService.districtFlow();
        cacheService.refreshCache("districtFlow", districtFlow);
        log.info("districtFlow save cache successfully");
    }

    // 行政区实时进出站客流，时效性普通
    private void trulyOnLine4NewDistrict(){
        Map trulyOnLine4NewDistrict = passengerFlowService.true4NewDistrict();
        cacheService.refreshCache("trulyOnLine4NewDistrict", trulyOnLine4NewDistrict);
        log.info("trulyOnLine4NewDistrict save cache successfully");
    }

    // 五大新城和虹桥商务区实时进出站客流，时效性普通
    private void newDistrictFlow(){
        Map<String, Object> newDistrictFlow = passengerFlowService.newDistrictFlow();
        cacheService.refreshCache("newDistrictFlow", newDistrictFlow);
        log.info("trulyOnLine4NewDistrict save cache successfully");
    }

    // 五大新城和虹桥商务区展示面板，时效性普通
    private void newDistrictDisplayBoard(){
        List<Map<String, Object>> newDistrictDisplayBoard
                = passengerFlowService.newDistrictDailyInAndOutFlow(passengerFlowService.queryNewDistrictWeather());
        cacheService.refreshCache("newDistrictDisplayBoard", newDistrictDisplayBoard);
        log.info("newDistrictDisplayBoard save cache successfully");
    }

    // 进博会专题，时效性普通
    private void aboutJB(){
        List<Map<String, Object>> aboutJB = passengerFlowService.jinBo();
        cacheService.refreshCache("aboutJB", aboutJB);
        log.info("aboutJB save cache successfully");
    }

    // 进博会专题，时效性普通
    private void monitor6station(){
        List<Map<String, Object>> monitor = passengerFlowService.monitor6station();
        cacheService.refreshCache("monitor6station", monitor);
        log.info("monitor6station save cache successfully");
    }

    // 总体规模，时效性普通
    private void scaleSummarize(){
        Map<String, Object> monitor = operateService.scaleSummarize();
        cacheService.refreshCache("scaleSummarize", monitor);
        log.info("scaleSummarize save cache successfully");
    }
}
